package com.kummuna.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.kummuna.R;
import com.kummuna.R.id;
import com.kummuna.R.layout;
import com.kummuna.R.menu;
import com.kummuna.adapter.SuggestListAdapter;
import com.kummuna.config.MainConfiguration;
import com.kummuna.util.json.JSONParser;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class DetectShortpath extends Activity implements LocationListener {

	private static LayoutInflater inflater = null;
	private static String url;
	// JSON Node Names
	public static final String TAG_ID = "Id";
	public static final String TAG_LatPoint = "LatPoint";
	public static final String TAG_LongPoint = "LongPoint";
	public static final String TAG_Status = "Status";
	public static final String TAG_UserId = "User_Id";
	public static final String TAG_Topic = "Topic";
	public static final String TAG_Detail = "Detail";
	public static final String TAG_Date = "Date";
	public static final String TAG_State = "State";
	public static final String TAG_Image = "Image";
	public static final String TAG_Distance = "Distance";

	public ArrayList<HashMap<String, String>> result;
	public ListView list;
	public SuggestListAdapter adapter;

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 100;

	GoogleMap map;
	private String latitudeFormDetectiveMap;
	private String longtitudeFormDetectiveMap;
	private LatLng latLng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detect_shortpath);

		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			latitudeFormDetectiveMap = extras.getString("latitudePosition");
			longtitudeFormDetectiveMap = extras.getString("longtitudePosition");
			
			String ltlng = latitudeFormDetectiveMap + ","
					+ longtitudeFormDetectiveMap;

			TextView latlong = (TextView) findViewById(R.id.txtlatlong);
			latlong.setText(ltlng);
		
			// call map
			map = ((MapFragment) getFragmentManager()
					.findFragmentById(R.id.map)).getMap();
			map.getUiSettings().setZoomControlsEnabled(false);
			map.getUiSettings().setScrollGesturesEnabled(false);

			latLng = new LatLng(Double.parseDouble(latitudeFormDetectiveMap),
					Double.parseDouble(longtitudeFormDetectiveMap));

			MarkerOptions markerOptions = new MarkerOptions();
			markerOptions.position(latLng);
			markerOptions.title(latLng.latitude + " : " + latLng.longitude);

			CameraPosition cameraPosition = new CameraPosition.Builder()
					.target(latLng).zoom(15).bearing(-30).tilt(90).build();

			// map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
			map.animateCamera(CameraUpdateFactory
					.newCameraPosition(cameraPosition));

			// Adding marker on the GoogleMap
			map.addMarker(markerOptions);
		}

		// call url config waterleak
		//MainConfiguration link = new MainConfiguration();
		url = MainConfiguration.addShortPath;
		url = url + "?lat=" + latitudeFormDetectiveMap + "&lon=" + longtitudeFormDetectiveMap;
	//    url = url+ "?lat=13&lon=14";
	//	Log.d("lattitude and longtitude Location Prevention: ",latitudeFormDetectiveMap +" "+ longtitudeFormDetectiveMap);
		result = new ArrayList<HashMap<String, String>>();

		// Getting JSON from URL
		JSONParser jParser = new JSONParser();
		JSONObject json = jParser.getJSONFromUrl(url);
		Log.d("Return Jason ShortPath : ", json.toString());
		try {
			JSONArray jArray = json.getJSONArray("Data");

			for (int i = 0; i < jArray.length(); i++) {

				HashMap<String, String> mapHash = new HashMap<String, String>();
				JSONObject json_data = jArray.getJSONObject(i);
				
				mapHash.put(TAG_ID, json_data.getString("Id"));
				mapHash.put(TAG_LatPoint, json_data.getString("LatPoint"));
				mapHash.put(TAG_LongPoint, json_data.getString("LongPoint"));
				mapHash.put(TAG_UserId, json_data.getString("User_id"));
				mapHash.put(TAG_Topic, json_data.getString("Topic"));
				mapHash.put(TAG_Detail, json_data.getString("Detail"));
		//		mapHash.put(TAG_Status, json_data.getString("Status"));
				mapHash.put(TAG_Date, json_data.getString("Date"));
		//		mapHash.put(TAG_State, json_data.getString("State"));
				mapHash.put(TAG_Image, json_data.getString("Image"));
				mapHash.put(TAG_Distance, json_data.getString("Distance"));
				
				Log.d("Distance", json_data.getString("Distance"));
				Log.d("Url", json_data.getString("Image"));
				
				result.add(mapHash);
			
			}

		}

		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

		list = (ListView) findViewById(R.id.list);
		adapter = new SuggestListAdapter(this, result);
		list.setAdapter(adapter);
		// adapter.notifyDataSetChanged();

		// Click event for single list row
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent i = new Intent();
				i.setClass(DetectShortpath.this, WaterLeakTabMain.class);

				// parameters
				i.putExtra("id", result.get(position).get(TAG_ID));
				i.putExtra("topic", result.get(position).get(TAG_Topic));
				i.putExtra("detail", result.get(position).get(TAG_Detail));
				i.putExtra("latitude", result.get(position).get(TAG_LatPoint));
				i.putExtra("longtitude", result.get(position).get(TAG_LongPoint));
				i.putExtra("distance", result.get(position).get(TAG_Distance));
				i.putExtra("date", result.get(position).get(TAG_Date));
				i.putExtra("url", result.get(position).get(TAG_Image));
				
				
				Log.d("intent image :", result.get(position).get(TAG_Image));
				Log.d("intent id :", result.get(position).get(TAG_ID));
				Log.d("intent topic :", result.get(position).get(TAG_Topic));
				Log.d("intent detail :", result.get(position).get(TAG_Detail));
				Log.d("intent lat :", result.get(position).get(TAG_LatPoint));
				// start the sample activity
				startActivity(i);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detect_shortpath, menu);
		return true;
	}

	public void nextpage(View view) {
		Intent mapIntent = new Intent(getApplicationContext(),
				WaterLeakTabMain.class);
		startActivity(mapIntent);
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		// Getting latitude of the current location
		double latitude = location.getLatitude();

		// Getting longitude of the current location
		double longitude = location.getLongitude();

		// Creating a LatLng object for the current location
		LatLng latLng = new LatLng(latitude, longitude);

		map.addMarker(new MarkerOptions().position(
				new LatLng(latitude, longitude)).title("Hello world"));
		map.getUiSettings().setZoomControlsEnabled(false);

		// Showing the current location in Google Map
		map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

		// Zoom in the Google Map
		map.animateCamera(CameraUpdateFactory.zoomTo(15));

	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
